package com.example.auv_660.util;

import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.PermissionChecker;

import com.example.auv_660.SplashActivity;

import java.util.ArrayList;
import java.util.List;

public class Permission {

    @SuppressLint("WrongConstant")
    public static boolean checkPermission(String[] permissions) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }

        for (String permission : permissions) {
            if (PermissionChecker.checkSelfPermission(SplashActivity.getInstance(), permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    @SuppressLint("WrongConstant")
    private static List<String> getDeniedPermission(String[] permissions) {
        List<String> needRequestPermissionList = new ArrayList<>();
        for (String permission : permissions) {
            if (PermissionChecker.checkSelfPermission(SplashActivity.getInstance(), permission) != PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.shouldShowRequestPermissionRationale(SplashActivity.getInstance(), permission)) {
                System.out.println(permission);
                needRequestPermissionList.add(permission);
            }
        }

        return needRequestPermissionList;
    }

    public static void requestPermission() {
        if (checkPermission(Constants.PERMISSIONS)) {

        } else {
            List<String> needPermissions = getDeniedPermission(Constants.PERMISSIONS);
            ActivityCompat.requestPermissions(SplashActivity.getInstance(), needPermissions.toArray(new String[needPermissions.size()]),
                    1);
        }
    }
}
